﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using HYI.SDBSERVER;
using eHCBaseClass;
using Sunny.UI;

namespace SHRSTOCK
{
    public partial class FMSITE : SDBSERVER.FMSINGLE
    {
        //入口函数
        public static Int32 StartMe(string sMainPara, string sPara, string sUser, DBSERVER sDB)
        {
            StaticClass.Selectm_MainPara = sMainPara;
            StaticClass.SelectItemValue = sPara;
            StaticClass.SelectUser = sUser;
            StaticClass.myDB = sDB;
            return 0;
        }

        private int iGridView = 0;
        private string sType = "V";
        private string sSite_No = string.Empty;
        //DBSERVER myADO = new DBSERVER();
        DBCache myCache = new DBCache();

        public FMSITE()
        {
            InitializeComponent();
        }

        //查询代码
        protected override void OnSys04()
        {
            string SQL = " select * from a_ts_site where 1=1";
            Qrytable = StaticClass.myDB.query(SQL);

            base.OnSys04();

            //表格列显示统一控制
            if (iGridView == 0)
            {
                myCache.doSetGridVIew(sMenu, sUser, uiDGView);
                iGridView = iGridView + 1;
            }
        }

        private void FMSITE_Load(object sender, EventArgs e)
        {
            _INPUTTABLE = "import_i_ts_point";
            sUser = StaticClass.SelectUser;
            string[] strArray = StaticClass.Selectm_MainPara.Split(new char[] { ';' });
            sMenu = strArray[1];
            sSite_No = strArray[0];

            //按钮控制
            sys01.Visible = false; sys01.Tag = "1";
            sys03.Visible = false; sys03.Tag = "1";
            sys04.Visible = false; sys04.Tag = "1";
            sys05.Visible = false; sys05.Tag = "1";
            sys06.Visible = false; sys06.Tag = "1";
            sys07.Visible = false; sys07.Tag = "1";
            sys08.Visible = false; sys08.Tag = "1";
            sys09.Visible = false; sys09.Tag = "1";
            sys11.Visible = false; sys11.Tag = "1";

            //执行查询代码
            OnSys04();
        }

        //编辑+浏览载入数据
        private void doLoadData()
        {
            if (!sType.Equals("A"))
            {
                uifsite_no.Text = uiDGView["fsite_no", rowindex].Value.ToString();
                uifsite_name.Text = uiDGView["fsite_name", rowindex].Value.ToString();
                uifengineer.Text = uiDGView["fengineer", rowindex].Value.ToString();
                uifphone.Text = uiDGView["fphone", rowindex].Value.ToString();
                uiftelno.Text = uiDGView["ftelno", rowindex].Value.ToString();

                string sday_off= uiDGView["day_off", rowindex].Value.ToString();
                string sfbegtime = uiDGView["fbegtime", rowindex].Value.ToString();
                string sfendtime = uiDGView["fendtime", rowindex].Value.ToString();

                string shour1 = sfbegtime.Substring(0, 2);
                string sminu1 = sfbegtime.Substring(3, 2);
                string shour2 = sfendtime.Substring(0, 2);
                string sminu2 = sfendtime.Substring(3, 2);

                if (cbhour1.Items.IndexOf(shour1) > -1)
                {
                    cbhour1.SelectedIndex = cbhour1.Items.IndexOf(shour1);
                }

                if (cbminu1.Items.IndexOf(sminu1) > -1)
                {
                    cbminu1.SelectedIndex = cbminu1.Items.IndexOf(sminu1);
                }

                if (cbhour2.Items.IndexOf(shour2) > -1)
                {
                    cbhour2.SelectedIndex = cbhour2.Items.IndexOf(shour2);
                }

                if (cbminu2.Items.IndexOf(sminu2) > -1)
                {
                    cbminu2.SelectedIndex = cbminu2.Items.IndexOf(sminu2);
                }

                uiCheckBox1.Checked = false;
                uiCheckBox2.Checked = false;
                uiCheckBox3.Checked = false;
                uiCheckBox4.Checked = false;
                if (sday_off.Substring(5, 1).Equals("1"))
                {
                    uiCheckBox1.Checked = true;
                }

                if (sday_off.Substring(6, 1).Equals("1"))
                {
                    uiCheckBox2.Checked = true;
                }

                if(uiDGView["fboxflag", rowindex].Value.ToString().Equals("1"))
                {
                    uiCheckBox3.Checked = true;
                }

                if (uiDGView["fuseflag", rowindex].Value.ToString().Equals("1"))
                {
                    uiCheckBox4.Checked = true;
                }

            }

        }

        //编辑代码
        protected override void OnSys02()
        {
            Console.WriteLine("行:" + rowindex.ToString());
            if (rowindex == -1)
            {
                return;
            }
            sType = "B";

            doLoadData();

            uifsite_no.ReadOnly = true;
            uifsite_name.ReadOnly = false;
            uifengineer.ReadOnly = false;
            uifphone.ReadOnly = false;
            uiftelno.ReadOnly = false;

            base.OnSys02();
        }

        //浏览代码
        protected override void OnSys10()
        {
            Console.WriteLine("行:" + rowindex.ToString());
            if (rowindex == -1)
            {
                return;
            }
            sType = "V";

            doLoadData();
            uifsite_no.ReadOnly = true;
            uifsite_name.ReadOnly = true;
            uifengineer.ReadOnly = true;
            uifphone.ReadOnly = true;
            uiftelno.ReadOnly = true;

            base.OnSys10();
        }

        //数据为空检查
        private bool CheckData()
        {
            return CheckEmpty(uifsite_name, "请输入客户名称")
                && CheckEmpty(uifengineer, "请输入联系人");
        }

        //保存代码
        protected override void OnSys08()
        {
            if (!CheckData())
            {
                return;
            }
            if (sType == "B")
            {
                using (XTBasEntities context = new XTBasEntities())
                {
                    //查询
                    a_ts_site myts_site = context.a_ts_site.Find(uifsite_no.Text);

                    myts_site.fsite_name = uifsite_name.Text;
                    myts_site.fengineer = uifengineer.Text;
                    myts_site.fphone = uifphone.Text;
                    myts_site.ftelno = uiftelno.Text;
                    myts_site.femail = uifemail.Text;
                    myts_site.foperator = sUser;
                    myts_site.fupddate = DateTime.Now;

                    string sbegtime = cbhour1.Text + ":" + cbminu1.Text;
                    string sendtime = cbhour2.Text + ":" + cbminu2.Text;
                    string sday_off = "00000";
                    if (uiCheckBox1.Checked)
                    {
                        sday_off = sday_off + "1";
                    }
                    else
                    {
                        sday_off = sday_off + "0";
                    }

                    if (uiCheckBox2.Checked)
                    {
                        sday_off = sday_off + "1";
                    }
                    else
                    {
                        sday_off = sday_off + "0";
                    }

                    int iboxflag = 0 ;
                    if (uiCheckBox3.Checked)
                    {
                        iboxflag = 1;
                    }

                    int iuseflag = 0;
                    if (uiCheckBox4.Checked)
                    {
                        iuseflag = 1;
                    }

                    myts_site.fboxflag = iboxflag;
                    myts_site.fuseflag = iuseflag;
                    myts_site.day_off = sday_off;
                    myts_site.fbegtime = sbegtime;
                    myts_site.fendtime = sendtime;

                    int result = context.SaveChanges();
                    UIMessageTip.ShowOk("修改成功");
                }
            }

            //刷新cache
            sType = "V";
            base.OnSys08();

            OnSys04();
        }
    }
}
